package jhh.servlet;

import java.awt.Font;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

import jhh.entity.Jtl;
@WebServlet("/PieChart")
public class PieChart extends HttpServlet {

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
           doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		 //创建主题样式  
	       StandardChartTheme standardChartTheme=new StandardChartTheme("CN");  
	       //设置标题字体  
	       standardChartTheme.setExtraLargeFont(new Font("隶书",Font.BOLD,20));  
	       //设置图例的字体  
	       standardChartTheme.setRegularFont(new Font("宋书",Font.PLAIN,15));  
	       //设置轴向的字体  
	       standardChartTheme.setLargeFont(new Font("宋书",Font.PLAIN,15));  
	       //应用主题样式  
	       ChartFactory.setChartTheme(standardChartTheme); 
         HttpSession session=request.getSession();
		 List<Jtl> list=(List<Jtl>) session.getAttribute("jtl");
		 int i=0,j=0;
		 System.out.println(list.size());
		 for (Jtl jtl : list) {
			if(jtl.isResult()==true){
				i++;
			}else{j++;}
		}
		// System.out.println(i+" "+j);
		 response.setContentType("image/png");
		 
		 PieDataset dataset=createPieDataset(i,j);
		 //创建图形对象
		 JFreeChart chart=ChartFactory.createPieChart3D("测试成功率", dataset, true, true,false);
		 PiePlot piePlot=(PiePlot) chart.getPlot();
		 piePlot.setLabelFont(new Font("宋体",0,12));
		 piePlot.setNoDataMessage("没有数据");
		 piePlot.setCircular(false);
		 piePlot.setLabelGap(0.02D);
		 ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart, 500, 270);
	}
    private PieDataset createPieDataset(int i,int j){
    	DefaultPieDataset dataset=new DefaultPieDataset();
    	dataset.setValue("成功", i);
    	dataset.setValue("失败", j);
    	return dataset;
    }
	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
